package com.cskaoyan.market.service.wx;

import com.cskaoyan.market.db.domain.MarketGoodsSpecification;
import com.cskaoyan.market.db.domain.MarketGoodsSpecificationExample;
import com.cskaoyan.market.db.mapper.MarketGoodsSpecificationMapper;
import com.cskaoyan.market.vo.SpecificationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class WxGoodsSpecificationServiceImpl implements WxGoodsSpecificationService {
    @Autowired
    MarketGoodsSpecificationMapper specificationMapper;

    @Override
    public List<SpecificationVo> querySpecification(Integer id) {
        List<SpecificationVo> data = new ArrayList<>();
        MarketGoodsSpecificationExample marketGoodsSpecificationExample = new MarketGoodsSpecificationExample();
        MarketGoodsSpecificationExample.Criteria criteria = marketGoodsSpecificationExample.createCriteria();
        criteria.andGoodsIdEqualTo(id);
        List<MarketGoodsSpecification> specificationList = specificationMapper.selectByExampleSelective(marketGoodsSpecificationExample);

        Map<String, List<MarketGoodsSpecification>> map = new HashMap<>();
        for (MarketGoodsSpecification marketGoodsSpecification : specificationList) {
            String specification = marketGoodsSpecification.getSpecification();
            List<MarketGoodsSpecification> valueList = map.get(specification);
            if (valueList == null) {
                valueList = new ArrayList<>();
            }
            valueList.add(marketGoodsSpecification);
            map.put(specification, valueList);
        }
        Set<Map.Entry<String, List<MarketGoodsSpecification>>> entries = map.entrySet();
        for (Map.Entry<String, List<MarketGoodsSpecification>> entry : entries) {
            SpecificationVo specificationVo = new SpecificationVo(entry.getKey(), entry.getValue());
            data.add(specificationVo);
        }
        return data;

    }
}

